package cn.stylefeng.guns.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.business.entity.WxUser;
import cn.stylefeng.guns.modular.business.exception.WxUserExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.WxUserMapper;
import cn.stylefeng.guns.modular.business.pojo.WxUserRequest;
import cn.stylefeng.guns.modular.business.service.WxUserService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 车辆管理业务实现层
 *
 * @author 施律名
 * @date 2022-03-10 17:48:59
 */
@Service
public class WxUserServicelmpl extends ServiceImpl<WxUserMapper, WxUser> implements WxUserService {

    @Override
    public void add(WxUserRequest wxUserRequest) {
        WxUser wxUser = new WxUser();
        BeanUtil.copyProperties(wxUserRequest, wxUser);
        this.save(wxUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(WxUserRequest wxUserRequest) {
        WxUser wxUser = this.queryWxUser(wxUserRequest);
        this.removeById(wxUser.getWxUserId());
    }

    @Override
    public void edit(WxUserRequest wxUserRequest) {
        WxUser wxUser = this.queryWxUser(wxUserRequest);
        BeanUtil.copyProperties(wxUserRequest, wxUser);
        this.updateById(wxUser);
    }

    @Override
    public WxUser detail(WxUserRequest wxUserRequest) {
        return this.queryWxUser(wxUserRequest);
    }

    @Override
    public PageResult<WxUser> findPage(WxUserRequest wxUserRequest) {
        LambdaQueryWrapper<WxUser> wrapper = createWrapper(wxUserRequest);
        Page<WxUser> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<WxUser> findList(WxUserRequest wxUserRequest) {
        LambdaQueryWrapper<WxUser> wrapper = this.createWrapper(wxUserRequest);
        return this.list(wrapper);
    }

    /**
     * 获取用户信息
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private WxUser queryWxUser(WxUserRequest wxUserRequest) {
        WxUser wxUser = this.getById(wxUserRequest.getWxUserId());
        if (ObjectUtil.isEmpty(wxUser)) {
            throw new BusinessException(WxUserExceptionEnum.WX_USER_EXCEPTION_ENUM);
        }
        return wxUser;
    }

    /**
     * 创建查询wrapper
     *
     * @author 施律名
     * @date 2022-03-10 17:48:59
     */
    private LambdaQueryWrapper<WxUser> createWrapper(WxUserRequest wxUserRequest) {
        LambdaQueryWrapper<WxUser> queryWrapper = new LambdaQueryWrapper<>();

        String wxUserStuName = wxUserRequest.getWxUserStuName();
        queryWrapper.like(ObjectUtil.isNotEmpty(wxUserStuName), WxUser::getWxUserStuName, wxUserStuName);

        // 根据时间倒序排列
        queryWrapper.orderByDesc(WxUser::getCreateTime);

        return queryWrapper;
    }


}